/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import javax.faces.bean.ViewScoped;
import javax.faces.bean.ManagedBean;
import session.stateless.StakeholderSessionBean;
import util.entityHelper.StakeholderAccountState;
import util.exception.ExistException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author jamie
 */
@ManagedBean
@ViewScoped //SessionScope
public class StakeholderLoginManagedBean implements Serializable {

    @EJB
    private StakeholderSessionBean stakeholderAccountMgt;
    private String username;
    private String password;

    public StakeholderLoginManagedBean() {
    }

    public void doLogin(ActionEvent event) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        try {
            StakeholderAccountState stakeholderState = stakeholderAccountMgt.getStakeholderByUsername(username);

            System.out.println("typed userName: " + username);
            System.out.println(password);
            if (stakeholderState == null) {
                // before login
                // EXCEPTION
                
                String statusMessage = "TOURIST_ACCT_EXISTS";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
            } else {
                // after login
                
                if (stakeholderState.getPassword().equals(password)) {
                    String statusMessage ="LOGIN_SUCCESS";
                    if(stakeholderState.isVerified()){
                    System.out.println("activation code: " + stakeholderState.isVerified());
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
                    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderAccountState", stakeholderState); 
                    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("stakeholderUsername", stakeholderState.getUsername());
            
                    // send stakeholder account ID to next page through http request
                    FacesContext.getCurrentInstance().getExternalContext().redirect("AddStakeholderPortalInfo.xhtml");// +stakeholderState.getId());
                    }//end of stakeholder is verified;
                    else{
                        statusMessage="Please check with t-cube on your account activation.";
                        System.out.println("activation code: " + stakeholderState.isVerified());
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage,""));
                    }
                    }
                else{
                    String statusMessage="WRONG PASSWORD or WRONG USERNAME";
                     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
                   
                }
            }

        } catch (ExistException ex) {
            // EXCEPTION
            String statusMessage = "LOGIN_FAIL";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
        catch(IOException ex){
             String statusMessage = "REDIRECT_FAIL";
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, statusMessage, ""));
        }
    }

    public void retrieveStakeholderInfo() {
        //this.setSelectedStakeholder(stakeholderSessionBean.retrieveStakeholderInfoByUsername(stakeholder.getUsername()));
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
}
